<?php

vendor("Simvis.InputModule");

class ConfigAction extends Action {

    function gen_cmdline($cfg) {
        if (!empty($cfg['inputfile'])) {
            $cmdline = $cfg['cmdline'] . " " . $cfg['program'] . " <" . $cfg['inputfile'];
        } else {
            $cmdline = $cfg['cmdline'] . " " . $cfg['program'];
        }

        return htmlentities($cmdline);
    }

    public function index() {
        $configs = D("Config");
        $modules = D("Module");
        $cfgs = $configs->where(1)->select();
        foreach ($cfgs as &$titem) {
            $titem['cmdline_show'] = $this->gen_cmdline($titem);
        }
        $this->items = $cfgs;
        $this->content = "Config:manager";
        $this->display("Public:layout");
    }

    public function view() {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Config/index");
        }
        $configs = D("Config");
        $modules = D("Module");
        $cfg = $configs->find($id);
        $mod = $modules->find($cfg['module_id']);
        $im = new InputModule();
        $im->prepare(("Template/{$mod['filename']}"));
        $configset = unserialize(convert_uudecode($cfg['configset']));
        $im->fill($configset);
        $this->cont = nl2br($im->generate($im->configset));
        $this->display("Public:layout");
    }

    public function delete() {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Config/index");
        }
        $configs = D("Config");
        if ($configs->delete($id)) {
            $_SESSION['message'] = "Config deleted!";
        } else {
            $_SESSION['message'] = "Delete failed!";
        }
        $this->redirect("Config/index");
    }

    public function rebuild() {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            $_SESSION['message'] = "Illegal invoke";
            $this->redirect("Config/index");
        }
        $configs = D("Config");
        $modules = D("Module");
        $cfg = $configs->find($id);
        $mod = $modules->find($cfg['module_id']);
        $im = new InputModule();
        $im->prepare(("Template/{$mod['filename']}"));
        $configset = unserialize(convert_uudecode($cfg['configset']));
        $im->fill($configset);
        $this->configset = $im->configset;
        $this->mod = $mod;
        $this->cfg = $cfg;
        $this->display("Public:layout");
    }

    public function rebuildsubmit() {
        $module_id = $_POST{'module_id'};
        $name = $_POST['configname'];
        $cmdline = $_POST['cmdline'];
        $program = $_POST['program'];
        $inputfile = $_POST['inputfile'];
        $id = $_POST['id'];
        if (empty($module_id) || empty($name) || empty($cmdline) || empty($program)) {
            $_SESSION['message'] = "Rebuild failed, you must fill in every blank!";
            $this->redirect("Config/index");
        }

        //Handle configset
        $configset = $_POST['set'];
        $im = new InputModule();
        $modules = D("Module");
        $mod = $modules->find($module_id);
        $im->prepare("Template/{$mod['filename']}");
        $im->fill($configset);
        $configset = convert_uuencode(serialize($im->configset));
        //End of configset
        //Handle attachment
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        $upload->allowExts = array("zip");
        $upload->uploadReplace = true;
        $result = $upload->upload("Attachment/");
        if ($result) {
            $_SESSION['message'] = "File uploaded!";
            $uploadinfo = $upload->getUploadFileInfo();
            $filename = $uploadinfo[0]['savename'];
        } else {
            $_SESSION['message'] = "File not uploaded!";
            $filename = "";
        }
        //end of attachment

        $configs = D("Config");

        $cfg = $configs->find($id);

        //fill data entry with post
        if ($filename != "") {
            $cfg['attachment'] = $filename;
        }

        $cfg['name'] = $name;
        $cfg['cmdline'] = $cmdline;
        $cfg['configset'] = $configset;
        $cfg['inputfile'] = $inputfile;
        $cfg['program'] = $program;


        //save into db
        if ($configs->data($cfg)->save()) {
            $_SESSION['message'] = "Rebuild compete!";
        } else {
            $_SESSION['message'] = "Rebuild failed!";
        }

        //redirect to testplan manager
        $this->redirect("Config/index");
    }

    public function build() {
        $modules = D("Module");
        $this->modules = $modules->where(1)->select();
        $this->display("Public:layout");
    }

    public function buildform() {
        if (empty($_POST['module_id'])) {
            $_SESSION['message'] = "Sorry you must choose one module first";
            $this->redirect("Config/build");
        } else {
            $module_id = $_POST['module_id'];
            $im = new InputModule();
            $modules = D("Module");
            $mod = $modules->find($module_id);
            $im->prepare("Template/{$mod[filename]}");
            $this->configset = $im->configset;
            $this->mod = $mod;
        }
        $this->display("Public:layout");
    }

    public function buildsubmit() {
        $module_id = $_POST{'module_id'};
        $name = $_POST['configname'];
        $cmdline = $_POST['cmdline'];
        $program = $_POST['program'];
        $inputfile = $_POST['inputfile'];

        if (empty($module_id) || empty($name) || empty($cmdline) || empty($program)) {
            $_SESSION['message'] = "Build failed, you must fill in every blank!";
            $this->redirect("Config/index");
        }

        //Handle configset
        $configset = $_POST['set'];
        $im = new InputModule();
        $modules = D("Module");
        $mod = $modules->find($module_id);
        $im->prepare("Template/{$mod[filename]}");
        $im->fill($configset);
        $configset = convert_uuencode(serialize($im->configset));
        //End of configset
        //Handle attachment
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        $upload->allowExts = array("zip");
        $upload->uploadReplace = true;
        $result = $upload->upload("Attachment/");
        if ($result) {
            $uploadinfo = $upload->getUploadFileInfo();
            $filename = $uploadinfo[0]['savename'];
        } else {
            $_SESSION['message'] = "File upload failed, you must attach the executable program and inputfile in your archive.";
            $filename = "";
        }
        //end of attachment



        $data = array(
            "module_id" => $module_id,
            "name" => $name,
            "cmdline" => $cmdline,
            "attachment" => $filename,
            "configset" => $configset,
            "program" => $program,
            "inputfile" => $inputfile
        );
        
        
        //judge attachement
        if (empty($filename)) {
            //$this->redirect("Config/index"); //for debug....
        }

        //judge testplan duplicate
        $configs = D("Config");
        $oldconfig = $configs->getByName($name);

        if (!empty($oldconfig)) {
            $_SESSION['message'] = "Sorry, the testplan with the same name exists, cannot create!";
        } else {
            if ($configs->data($data)->add()) {
                $_SESSION['message'] = "Build complete!";
            } else {
                $_SESSION['message'] = "Build failed";
            }
        }
        $this->redirect("Config/index");
    }

}

?>